<?php
declare (strict_types = 0);
namespace app\controller;

use think\facade\View;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use app\model\TableModel;

//餐桌控制器
class Table{

    //常用数据表内容 及常用字典
    var $goodsType=[];
    var $dept=[];
    var $users =[];
    var $status=array('1'=>'空闲','2'=>'停用','3'=>'用餐中');

    public function __construct(){

        $list = Db::table("user")->select()->toArray();
        foreach($list as $k=>$v){
            $this->users[$v['id']]=$v;
        }        
    }

    

    //类别
    public function index(){
        $list = Db::table("table")->where(1)->paginate(15)->toArray();
        $type = ['1'=>'圆桌','2'=>'方桌','3'=>'卡座','4'=>'包厢'];

        foreach($list['data'] as &$kv){
            $kv['type_name'] = $type[$kv['type']];  
            $kv['status_name'] = $this->status[$kv['status']];
            $kv['creater_name'] = $this->users[$kv['creater']]['name'];
        }

        View::assign(['list'=>$list]);

        return View::fetch();
    }  


    //分类编辑方法
    public function edit(){
        $all = Request::param();
        $id=$all['id'];

        if(empty($id)) return 'Invalid id!' ;
        $table = new TableModel();

        //传入内容,保存数据并跳转
        if($id>0 && !empty($all['name'])){
            $all['create_time'] = date('Y-m-d H:i:s');
            Db::table('table')->save($all);
            header('location:/index.php/table');
        }
        //不传内容只有id,查出本条数据 并进入编辑
        $row = $table->find($id);

        View::assign(['data'=>$row]);
        return View::fetch('edit');
    }

    //分类新增
    public function table_add(){
        $all = Request::param();
        if($all){
            if(!empty($all['id'])) return 'It is not a new table!';     
            if(!$all['name']) return 'Invalid table name!';

            $table = new TableModel();
            $row = $table::where('name',$all['name'])->find();
            if(!empty($row)) return '餐桌已存在! Table exist! ['. $all['name']."]";

            $all['creater']  = Session::get('logonid');
            $all['create_time']  = date("Y-m-d H:i:s",time());
            
            $table->insert($all);
            header("location:/index.php/table");
        }
        //View::assign();
        return View::fetch('table_add');
    }

  

}
